package com.css.dzj.dzjap.modules.lawenforcesuper.service.impl;

import com.css.dzj.dzjap.common.util.*;
import com.css.dzj.dzjap.modules.lawenforcesuper.entity.*;
import com.css.dzj.dzjap.modules.lawenforcesuper.mapper.DrawRelationTaskMapper;
import com.css.dzj.dzjap.modules.lawenforcesuper.mapper.SuperTaskMapper;
import com.css.dzj.dzjap.modules.lawenforcesuper.service.SuperTaskService;
import com.css.dzj.dzjap.modules.lawenforcesuper.view.ApDeptExtractView;
import com.css.dzj.dzjap.modules.lawenforcesuper.view.SuperviseExtractRecordGainView;
import com.css.dzj.dzjap.modules.lawenforcesuper.view.SuperviseExtractRecordView;
import com.css.dzj.dzjap.modules.publick.dao.PublicDao;
import com.css.dzj.dzjap.modules.publick.entity.ApPublicEntity;
import com.css.dzj.dzjap.modules.record.approject.entity.ApRecordEntity;
import com.css.dzj.dzjap.modules.record.approject.service.ApRecordService;
import com.css.dzj.dzjap.modules.record.community.entity.ApRecordGainEntity;
import com.css.dzj.dzjap.modules.record.community.service.CommunityRecordService;
import com.css.dzj.dzjap.modules.todolist.entity.ApTodoEntity;
import com.css.dzj.dzjap.modules.todolist.service.ApTodoService;
import com.css.dzj.dzjap.sys.user.entity.LoginUser;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
/**
 * 项目抽出service实现类
 * @author limanman
 */
@Service
public class SuperTaskServiceImpl implements SuperTaskService {
    @Autowired
    SuperTaskMapper superTaskMapper;
    @Autowired
    DrawRelationTaskMapper drawRelationTaskMapper;
    @Autowired
    CommunityRecordService communityRecordService;
    @Autowired
    ApRecordService apRecordService;
    @Autowired
    private  JPAQueryFactory jpaQueryFactory;
    // 公开公告Dao
    @Autowired
    private PublicDao publicDao;
    @Autowired
    private ServiceUtil serviceUtil;
    @Autowired
    private ApTodoService apTodoService;
    /**
     * 添加两张表
     * @param type
     * @param apSupersiveTaskInfoEntity
     * @return
     */
    @Override
    @Transactional
    public R saveTaskInfo(String type,ApSupersiveTaskInfoEntity apSupersiveTaskInfoEntity,HttpServletRequest request) {
        try {
            if(StringUtils.isEmpty(apSupersiveTaskInfoEntity)){
                throw new RuntimeException("新增不能为空");
            }
            /**
             * 获取suser
             */
            R r = serviceUtil.getUserInfoByRequest(request);
            String suserName = "";
            String location = "";
            String address="";
            String userUnit ="";
            if(r.getCode().equals("200")){
                LoginUser userInfo =  (LoginUser)r.get("data");
                /**
                 * 获取用户名
                 */
                suserName =userInfo.getLoginName();
                /**
                 * 获取用户的省份
                 */
                String orgName = userInfo.getOrgName();
                location = orgName.replace("地震局","");
                address = userInfo.getAddress();
                userUnit= userInfo.getOrgName();
            }

            apSupersiveTaskInfoEntity.setId(UUID.randomUUID().toString());
            apSupersiveTaskInfoEntity.setCreateTime(new Date());
            apSupersiveTaskInfoEntity.setCreatePerson(suserName);
            apSupersiveTaskInfoEntity.setCreatePersonLocation(location);
            apSupersiveTaskInfoEntity.setSuperType(type);
            apSupersiveTaskInfoEntity.setEnterpriseNotice("0");
            String apSupersiveTaskInfoId = apSupersiveTaskInfoEntity.getId();

            /**
             * 抽取的总数
             */
            Integer superTaskNum = apSupersiveTaskInfoEntity.getSuperTaskNum();

            if (superTaskNum<=0){
                throw new RuntimeException("抽取安评项目总数不能小于0");
                //return R.error(500,"抽取安评单位总数不能为0");
            }

            if (apSupersiveTaskInfoEntity.getSuperTaskProporState()+apSupersiveTaskInfoEntity.getSuperTaskProporProvin()+apSupersiveTaskInfoEntity.getSuperTaskProporLocal()!=100){
                throw new RuntimeException("抽取比例错误，抽取比例总和应为100%！");
            }

            double num=100.0;
            /***
             * 国家级抽取比例
             */
            double superTaskProporState=(apSupersiveTaskInfoEntity.getSuperTaskProporState()/num)*superTaskNum;
            Integer country = Integer.parseInt(new BigDecimal(superTaskProporState).setScale(0,BigDecimal.ROUND_HALF_DOWN).toString());
            double dc = superTaskProporState - country;
            int i=0;
            if (dc == 0.5){
                i++;
            }
            Integer superState = Integer.parseInt(new BigDecimal(superTaskProporState).setScale(0,BigDecimal.ROUND_HALF_UP).toString());
            /***
             * 省级抽取比例
             */
            double superTaskProporProvin=(apSupersiveTaskInfoEntity.getSuperTaskProporProvin()/num)*superTaskNum;
            Integer provience =Integer.parseInt(new BigDecimal(superTaskProporProvin).setScale(0,BigDecimal.ROUND_HALF_DOWN).toString());
            Integer superProvin = 0;
            if (superTaskProporProvin-provience == 0.5){
                if (i>0){
                    superProvin = Integer.parseInt(new BigDecimal(superTaskProporProvin).setScale(0,BigDecimal.ROUND_HALF_DOWN).toString());

                }
            }else {
                superProvin =Integer.parseInt(new BigDecimal(superTaskProporProvin).setScale(0,BigDecimal.ROUND_HALF_UP).toString());
            }
            /***
             *地方级抽取比例
             */
            Integer superLocal = superTaskNum-superProvin-superState;


            List<DrawTaskRelationRecordEntity> drawTaskRelationRecordEntities = new ArrayList<>();
            DrawTaskRelationRecordEntity drawTaskRelationRecordEntity;

            int randomNum=0;
            /***
             * 备案类型： 0.区域地震安全性评估成果1.地震小区划工作成果2.安评工作成果
             */
            if(type.equals(Constant.REGINAL_EARTHQU)){
                /***
                 * 国家级的获取区域安全性评估成果,数据库数量
                 */
                List<ApRecordGainEntity> listState=communityRecordService.getApRecordGainListByRecordLevel(location,Constant.REGINAL_EARTHQU,Constant.STATE_LEVEL);
                if(superState<=listState.size()){
                    List<ApRecordGainEntity> listStateLevel = RandomUtil.ramdonExtract(listState, superState.toString());
                    if(!StringUtils.isEmpty(listStateLevel) && listStateLevel.size()>0){
                        for (ApRecordGainEntity apRecordGainEntity:listStateLevel) {
                            drawTaskRelationRecordEntity=new DrawTaskRelationRecordEntity();
                            drawTaskRelationRecordEntity.setRecordId(apRecordGainEntity.getRecordId());
                            drawTaskRelationRecordEntity.setApSuperTaskId(apSupersiveTaskInfoId);
                            drawTaskRelationRecordEntity.setId(UUID.randomUUID().toString());
                            drawTaskRelationRecordEntity.setCreateTime(new Date());
                            drawTaskRelationRecordEntity.setCreatePerson(suserName);
                            drawTaskRelationRecordEntity.setCqStatus(Constant.UNEXTRACT);
                            drawTaskRelationRecordEntity.setSuperType(Constant.REGINAL_EARTHQU);
                            drawTaskRelationRecordEntities.add(drawTaskRelationRecordEntity);
                            randomNum++;
                        }
                    }
                }else{
                    throw new RuntimeException("符合抽取条件的国家级区域地震安全性评估成果不足！");
                }
                /***
                * 省级的获取区域安全性评估成果
                */
                List<ApRecordGainEntity> listProvin=communityRecordService.getApRecordGainListByRecordLevel(location,Constant.REGINAL_EARTHQU,Constant.PROVIENCE_LEVEL);
                /***
                 * 抽取到省级的项目备案
                 */
                if (superProvin<=listProvin.size()){
                    List<ApRecordGainEntity> listProvinLevel= RandomUtil.ramdonExtract(listProvin,superProvin.toString());
                    if(!StringUtils.isEmpty(listProvinLevel)&&listProvinLevel.size()>0){
                        for (ApRecordGainEntity apRecordGainEntity:listProvinLevel) {
                            drawTaskRelationRecordEntity=new DrawTaskRelationRecordEntity();
                            drawTaskRelationRecordEntity.setId(UUID.randomUUID().toString());
                            drawTaskRelationRecordEntity.setRecordId(apRecordGainEntity.getRecordId());
                            drawTaskRelationRecordEntity.setApSuperTaskId(apSupersiveTaskInfoId);
                            drawTaskRelationRecordEntity.setCreateTime(new Date());
                            drawTaskRelationRecordEntity.setCreatePerson(suserName);
                            drawTaskRelationRecordEntity.setCqStatus(Constant.UNEXTRACT);
                            drawTaskRelationRecordEntity.setSuperType(Constant.REGINAL_EARTHQU);
                            drawTaskRelationRecordEntities.add(drawTaskRelationRecordEntity);
                            randomNum++;
                        }
                    }
                }else {
                    throw new RuntimeException("符合抽取条件的省级区域地震安全性评估成果不足！");
                }

                /***
                 * 地方级的获取区域安全性评估成果
                 */
                List<ApRecordGainEntity> listCity=communityRecordService.getApRecordGainListByRecordLevel(location,Constant.REGINAL_EARTHQU,Constant.CITY_LEVEL);
                /***
                 * 抽取到地方级的项目备案
                 */
                if(superLocal<=listCity.size()) {
                    List<ApRecordGainEntity> listLocalLevel = RandomUtil.ramdonExtract(listCity, superLocal.toString());
                    if(!StringUtils.isEmpty(listLocalLevel)&&listLocalLevel.size()>0){
                        for (ApRecordGainEntity apRecordGainEntity:listLocalLevel) {
                            drawTaskRelationRecordEntity=new DrawTaskRelationRecordEntity();
                            drawTaskRelationRecordEntity.setId(UUID.randomUUID().toString());
                            drawTaskRelationRecordEntity.setRecordId(apRecordGainEntity.getRecordId());
                            drawTaskRelationRecordEntity.setApSuperTaskId(apSupersiveTaskInfoId);
                            drawTaskRelationRecordEntity.setCreateTime(new Date());
                            drawTaskRelationRecordEntity.setCreatePerson(suserName);
                            drawTaskRelationRecordEntity.setCqStatus(Constant.UNEXTRACT);
                            drawTaskRelationRecordEntity.setSuperType(Constant.REGINAL_EARTHQU);
                            drawTaskRelationRecordEntities.add(drawTaskRelationRecordEntity);
                            randomNum++;
                        }
                    }
                }else {
                    throw new RuntimeException("符合抽取条件的地市级区域地震安全性评估成果不足！");
                    //return R.error("地方级抽取比例大于地方级总数");
                }

                if(randomNum!=superTaskNum){
                    throw new  RuntimeException("实际抽取数量与预期抽取数量不符！");
                }
            }else if(type.equals(Constant.EARTHQU_SUBDIVI)){

                /***
                 * 国家级的获取地震小区划
                 */
                List<ApRecordGainEntity> listState=communityRecordService.getApRecordGainListByRecordLevel(location,Constant.EARTHQU_SUBDIVI,Constant.STATE_LEVEL);
                /**
                 *抽取到国家级的项目备案判断抽取数量和国家级的总数
                 */
                if(superState<=listState.size()) {
                    List<ApRecordGainEntity> listStateLevel = RandomUtil.ramdonExtract(listState, superState.toString());
                    if(!StringUtils.isEmpty(listStateLevel) && listStateLevel.size()>0){
                        for (ApRecordGainEntity apRecordGainEntity:listStateLevel) {
                            drawTaskRelationRecordEntity=new DrawTaskRelationRecordEntity();
                            drawTaskRelationRecordEntity.setRecordId(apRecordGainEntity.getRecordId());
                            drawTaskRelationRecordEntity.setApSuperTaskId(apSupersiveTaskInfoId);
                            drawTaskRelationRecordEntity.setId(UUID.randomUUID().toString());
                            drawTaskRelationRecordEntity.setCreateTime(new Date());
                            drawTaskRelationRecordEntity.setCreatePerson(suserName);
                            drawTaskRelationRecordEntity.setCqStatus(Constant.UNEXTRACT);
                            drawTaskRelationRecordEntity.setSuperType(Constant.EARTHQU_SUBDIVI);
                            drawTaskRelationRecordEntities.add(drawTaskRelationRecordEntity);
                            randomNum++;
                        }
                    }
                }else {
                    throw new RuntimeException("符合抽取条件的国家级地震小区划工作成果不足！");
                    //return R.error("国家级抽取比例超过国家级总数");
                }

                /***
                 * 省级的获取区域安全性评估成果
                 */
                List<ApRecordGainEntity> listProvin=communityRecordService.getApRecordGainListByRecordLevel(location,Constant.EARTHQU_SUBDIVI,Constant.PROVIENCE_LEVEL);
                /***
                 * 抽取到省级的项目备案
                 */
                if (superProvin<=listProvin.size()){
                    List<ApRecordGainEntity> listProvinLevel= RandomUtil.ramdonExtract(listProvin,superProvin.toString());
                    if(!StringUtils.isEmpty(listProvinLevel)&&listProvinLevel.size()>0){
                        for (ApRecordGainEntity apRecordGainEntity:listProvinLevel) {
                            drawTaskRelationRecordEntity=new DrawTaskRelationRecordEntity();
                            drawTaskRelationRecordEntity.setId(UUID.randomUUID().toString());
                            drawTaskRelationRecordEntity.setRecordId(apRecordGainEntity.getRecordId());
                            drawTaskRelationRecordEntity.setApSuperTaskId(apSupersiveTaskInfoId);
                            drawTaskRelationRecordEntity.setCreateTime(new Date());
                            drawTaskRelationRecordEntity.setCreatePerson(suserName);
                            drawTaskRelationRecordEntity.setCqStatus(Constant.UNEXTRACT);
                            drawTaskRelationRecordEntity.setSuperType(Constant.EARTHQU_SUBDIVI);
                            drawTaskRelationRecordEntities.add(drawTaskRelationRecordEntity);
                            randomNum++;
                        }
                    }
                }else {
                    throw new RuntimeException("符合抽取条件的省级地震小区划工作成果不足！");
                    //return R.error("省级抽取比例超过省级备案总数");
                }

                /***
                 * 地方级的获取区域安全性评估成果
                 */
                List<ApRecordGainEntity> listCity=communityRecordService.getApRecordGainListByRecordLevel(location,Constant.EARTHQU_SUBDIVI,Constant.CITY_LEVEL);
                /***
                 * 抽取到地方级的项目备案
                 */
                if(superLocal<=listCity.size()) {
                    List<ApRecordGainEntity> listLocalLevel = RandomUtil.ramdonExtract(listCity, superLocal.toString());
                    if(!StringUtils.isEmpty(listLocalLevel)&&listLocalLevel.size()>0){
                        for (ApRecordGainEntity apRecordGainEntity:listLocalLevel) {
                            drawTaskRelationRecordEntity=new DrawTaskRelationRecordEntity();
                            drawTaskRelationRecordEntity.setId(UUID.randomUUID().toString());
                            drawTaskRelationRecordEntity.setRecordId(apRecordGainEntity.getRecordId());
                            drawTaskRelationRecordEntity.setApSuperTaskId(apSupersiveTaskInfoId);
                            drawTaskRelationRecordEntity.setCreateTime(new Date());
                            drawTaskRelationRecordEntity.setCreatePerson(suserName);
                            drawTaskRelationRecordEntity.setCqStatus(Constant.UNEXTRACT);
                            drawTaskRelationRecordEntity.setSuperType(Constant.EARTHQU_SUBDIVI);
                            drawTaskRelationRecordEntities.add(drawTaskRelationRecordEntity);
                            randomNum++;
                        }
                    }
                }else {
                    throw new RuntimeException("符合抽取条件的地市级地震小区划工作成果不足！");
                   // return R.error("地方级抽取比例大于地方级总数");
                }
                if(randomNum!=superTaskNum){
                    throw new  RuntimeException("实际抽取数量与预期抽取数量不符！");
                }
            }else if(type.equals(Constant.AP_WORK)){
                /***
                 * 获取国家级总数
                 */
                List<ApRecordEntity> liststate=apRecordService.getApRecordListByRecordLevel(location,Constant.STATE_LEVEL);
                /***
                 *随机抽取国家级
                 */
                if(superState<=liststate.size()){
                    List<ApRecordEntity> liststateLevel= RandomUtil.ramdonExtract(liststate,superState.toString());
                    if(!StringUtils.isEmpty(liststateLevel)&& liststateLevel.size()>0){
                        for (ApRecordEntity apRecordEntity: liststateLevel) {
                            drawTaskRelationRecordEntity=new DrawTaskRelationRecordEntity();
                            drawTaskRelationRecordEntity.setId(UUID.randomUUID().toString());
                            drawTaskRelationRecordEntity.setRecordId(apRecordEntity.getRecordId());
                            drawTaskRelationRecordEntity.setApSuperTaskId(apSupersiveTaskInfoId);
                            drawTaskRelationRecordEntity.setCreateTime(new Date());
                            drawTaskRelationRecordEntity.setCreatePerson(suserName);
                            drawTaskRelationRecordEntity.setCqStatus(Constant.UNEXTRACT);
                            drawTaskRelationRecordEntity.setSuperType(Constant.AP_WORK);
                            drawTaskRelationRecordEntities.add(drawTaskRelationRecordEntity);
                            randomNum++;
                        }
                    }
                }else {
                    throw new RuntimeException("符合抽取条件的国家级安评工作成果不足！");
                }
                /***
                 * 获取省级总数
                 */
                List<ApRecordEntity> listprovien=apRecordService.getApRecordListByRecordLevel(location,Constant.PROVIENCE_LEVEL);
                /***
                 * 抽取省级数量
                 */
                if(superProvin<=listprovien.size()){
                    List<ApRecordEntity> listProvienLevel=RandomUtil.ramdonExtract(listprovien,superProvin.toString());
                    if(!StringUtils.isEmpty(listProvienLevel) && listProvienLevel.size()>0){
                        for (ApRecordEntity apRecordEntity: listProvienLevel) {
                            drawTaskRelationRecordEntity=new DrawTaskRelationRecordEntity();
                            drawTaskRelationRecordEntity.setId(UUID.randomUUID().toString());
                            drawTaskRelationRecordEntity.setRecordId(apRecordEntity.getRecordId());
                            drawTaskRelationRecordEntity.setApSuperTaskId(apSupersiveTaskInfoId);
                            drawTaskRelationRecordEntity.setCreateTime(new Date());
                            drawTaskRelationRecordEntity.setCreatePerson(suserName);
                            drawTaskRelationRecordEntity.setCqStatus(Constant.UNEXTRACT);
                            drawTaskRelationRecordEntity.setSuperType(Constant.AP_WORK);
                            drawTaskRelationRecordEntities.add(drawTaskRelationRecordEntity);
                            randomNum++;
                        }
                    }
                }else {
                    throw new RuntimeException("符合抽取条件的省级安评工作成果不足！");
                    //return R.error("抽取比例总数大于总人数");
                }

                /***
                * 获取地方级数量
                */
                List<ApRecordEntity> listCity=apRecordService.getApRecordListByRecordLevel(location,Constant.CITY_LEVEL);
                /***
                 * 抽取地方级数量
                 */
                if(superLocal<=listCity.size()){
                    List<ApRecordEntity> listCityLevel=RandomUtil.ramdonExtract(listCity,superLocal.toString());
                    if(!StringUtils.isEmpty(listCityLevel) && listCityLevel.size()>0){
                        for (ApRecordEntity apRecordEntity: listCityLevel) {
                            drawTaskRelationRecordEntity=new DrawTaskRelationRecordEntity();
                            drawTaskRelationRecordEntity.setId(UUID.randomUUID().toString());
                            drawTaskRelationRecordEntity.setRecordId(apRecordEntity.getRecordId());
                            drawTaskRelationRecordEntity.setApSuperTaskId(apSupersiveTaskInfoId);
                            drawTaskRelationRecordEntity.setCreateTime(new Date());
                            drawTaskRelationRecordEntity.setCreatePerson(suserName);
                            drawTaskRelationRecordEntity.setCqStatus(Constant.UNEXTRACT);
                            drawTaskRelationRecordEntity.setSuperType(Constant.AP_WORK);
                            drawTaskRelationRecordEntities.add(drawTaskRelationRecordEntity);
                            randomNum++;
                        }
                    }
                }else {
                    throw new RuntimeException("符合抽取条件的地市级安评工作成果不足！");
                    //return R.error("抽取比例总数大于总人数");
                }
                if(randomNum!=superTaskNum){
                    throw new  RuntimeException("实际抽取数量与预期抽取数量不符！");
                }
            }

            if(!StringUtils.isEmpty(drawTaskRelationRecordEntities) && drawTaskRelationRecordEntities.size()>0){
                drawRelationTaskMapper.saveAll(drawTaskRelationRecordEntities);
            }

            superTaskMapper.save(apSupersiveTaskInfoEntity);

            return R.ok("保存成功！").put("taskId",apSupersiveTaskInfoId);
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 根据标题和类型查询安评项目抽取信息表
     * @param pageable
     * @param taskTitle
     * @param type
     * @return
     */
    @Override
    public QueryResults<ApSupersiveTaskInfoEntity> queryApSupersiveTaskInfo(Pageable pageable, String taskTitle, String type,HttpServletRequest request) {
        try {
            /**
             * 获取suser
             */
            R r = serviceUtil.getUserInfoByRequest(request);
            String suserName = "";
            String location = "";
            String address="";
            String userUnit ="";
            if(r.getCode().equals("200")){
                LoginUser userInfo =  (LoginUser)r.get("data");
                /**
                 * 获取用户名
                 */
                suserName =userInfo.getLoginName();
                /**
                 * 获取用户的省份
                 */
                String orgName = userInfo.getOrgName();
                location = orgName.replace("地震局","");
                address = userInfo.getAddress();
                userUnit= userInfo.getOrgName();
            }

            if (location!=null && !"".equals(location)){
                QApSupersiveTaskInfoEntity qApSupersiveTaskInfoEntity = QApSupersiveTaskInfoEntity.apSupersiveTaskInfoEntity;
                Predicate predicate = qApSupersiveTaskInfoEntity.isNotNull().or(qApSupersiveTaskInfoEntity.isNull());
                predicate = ("".equals(taskTitle) || taskTitle==null)?predicate: ExpressionUtils.and(predicate,qApSupersiveTaskInfoEntity.superTitle.like("%"+taskTitle+"%"));
                predicate = ("".equals(type) || type==null)?predicate: ExpressionUtils.and(predicate,qApSupersiveTaskInfoEntity.superType.eq(type));
                if (!Constant.LOCATIONZG.equals(location)){
                    predicate = ("".equals(location)||location==null)?predicate : ExpressionUtils.and(predicate,qApSupersiveTaskInfoEntity.createPersonLocation.like("%"+location+"%"));
                }
                QueryResults<ApSupersiveTaskInfoEntity> page=jpaQueryFactory.selectFrom(qApSupersiveTaskInfoEntity)
                        .where(predicate)
                        .orderBy(qApSupersiveTaskInfoEntity.createTime.desc())
                        .offset(pageable.getOffset())
                        .limit(pageable.getPageSize())
                        .fetchResults();
                QueryResults queryResults  = new QueryResults(page.getResults(),page.getLimit(),page.getOffset(),page.getTotal());
                return queryResults;
            }else {
                throw new RuntimeException("用户所在省份不能为空！");
            }

        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }


    /**
     * 根据标题和类型查询所有任务
     * @param pageable
     * @param taskTitle
     * @param type
     * @return
     */
    @Override
    public QueryResults<ApSupersiveTaskInfoEntity> queryAllApSupersiveTaskInfo(Pageable pageable, String taskTitle, String type,HttpServletRequest request) {
        try {
            /**
             * 获取suser
             */
            R r = serviceUtil.getUserInfoByRequest(request);
            String suserName = "";
            String location = "";
            String address="";
            String userUnit ="";
            if(r.getCode().equals("200")){
                LoginUser userInfo =  (LoginUser)r.get("data");
                /**
                 * 获取用户名
                 */
                suserName =userInfo.getLoginName();
                /**
                 * 获取用户的省份
                 */
                String orgName = userInfo.getOrgName();
                location = orgName.replace("地震局","");
                address = userInfo.getAddress();
                userUnit= userInfo.getOrgName();
            }

            QApSupersiveTaskInfoEntity qApSupersiveTaskInfoEntity = QApSupersiveTaskInfoEntity.apSupersiveTaskInfoEntity;
            Predicate predicate = qApSupersiveTaskInfoEntity.isNotNull().or(qApSupersiveTaskInfoEntity.isNull());
            predicate = ("".equals(taskTitle) || taskTitle==null)?predicate: ExpressionUtils.and(predicate,qApSupersiveTaskInfoEntity.superTitle.like("%"+taskTitle+"%"));
            predicate = ("".equals(type) || type==null)?predicate: ExpressionUtils.and(predicate,qApSupersiveTaskInfoEntity.superType.eq(type));
            QueryResults<ApSupersiveTaskInfoEntity> page=jpaQueryFactory.selectFrom(qApSupersiveTaskInfoEntity)
                    .where(predicate)
                    .orderBy(qApSupersiveTaskInfoEntity.createTime.desc())
                    .offset(pageable.getOffset())
                    .limit(pageable.getPageSize())
                    .fetchResults();
            QueryResults queryResults  = new QueryResults(page.getResults(),page.getLimit(),page.getOffset(),page.getTotal());
            return queryResults;

        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 根据id查抽取询监管任务信息表
     * @param taskId
     * @return
     */
    @Override
    public ApSupersiveTaskInfoEntity queryApSupersiveTaskInfoById(String taskId) {
        if(!"".equals(taskId) && taskId!=null){
            QApSupersiveTaskInfoEntity qApSupersiveTaskInfoEntity = QApSupersiveTaskInfoEntity.apSupersiveTaskInfoEntity;
            return  jpaQueryFactory.selectFrom(qApSupersiveTaskInfoEntity)
                    .where(qApSupersiveTaskInfoEntity.id.eq(taskId))
                    .fetchOne();
        }else {
            return  null;
        }
    }

    /**
     * 中间表
     * @param pageable
     * @param taskId
     * @return
     */
    @Override
    public QueryResults<DrawTaskRelationRecordEntity> queryTaskRelationByTaskId(Pageable pageable, String taskId) {
        try {
            QDrawTaskRelationRecordEntity qDrawTaskRelationRecordEntity = QDrawTaskRelationRecordEntity.drawTaskRelationRecordEntity;
            Predicate predicate = qDrawTaskRelationRecordEntity.isNotNull().or(qDrawTaskRelationRecordEntity.isNull());
            predicate = ("".equals(taskId) || taskId==null)?predicate : ExpressionUtils.and(predicate ,qDrawTaskRelationRecordEntity.apSuperTaskId.eq(taskId));

            return  jpaQueryFactory.selectFrom(qDrawTaskRelationRecordEntity)
                    .where(predicate)
                    .offset(pageable.getOffset())
                    .limit(pageable.getPageSize())
                    .fetchResults();

        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }

    }

    /**
     * 查询抽出的项目
     * @param pageable
     * @param taskId
     * @return
     */
    @Override
    public QueryResults queryApRecord(Pageable pageable, String taskId,HttpServletRequest request) {
        try {
            /**
             * 获取suser
             */
            R r = serviceUtil.getUserInfoByRequest(request);
            String suserName = "";
            String location = "";
            String address="";
            String userUnit ="";
            if(r.getCode().equals("200")){
                LoginUser userInfo =  (LoginUser)r.get("data");
                /**
                 * 获取用户名
                 */
                suserName =userInfo.getLoginName();
                /**
                 * 获取用户的省份
                 */
                String orgName = userInfo.getOrgName();
                location = orgName.replace("地震局","");
                address = userInfo.getAddress();
                userUnit= userInfo.getOrgName();
            }
            if (taskId!=null && !"".equals(taskId)){
                QueryResults<DrawTaskRelationRecordEntity> page = queryTaskRelationByTaskId(pageable,taskId);
                List<DrawTaskRelationRecordEntity> drawTaskRelationRecordEntities=page.getResults();
                ApRecordGainEntity apRecordGainEntity;
                List<ApRecordGainEntity> apRecordGainEntities = new ArrayList<>();
                ApRecordEntity apRecordEntity;
                List<ApRecordEntity> apRecordEntities = new ArrayList<>();

                SuperviseExtractRecordGainView superviseExtractRecordGainView;
                SuperviseExtractRecordView superviseExtractRecordView;
                List<SuperviseExtractRecordGainView> superviseExtractRecordGainViews = new ArrayList<>();
                List<SuperviseExtractRecordView> superviseExtractRecordViews = new ArrayList<>();
                for(DrawTaskRelationRecordEntity drawTaskRelationRecordEntity:drawTaskRelationRecordEntities){
                    if (drawTaskRelationRecordEntity.getSuperType().equals(Constant.REGINAL_EARTHQU) || drawTaskRelationRecordEntity.getSuperType().equals(Constant.EARTHQU_SUBDIVI)){
                        apRecordGainEntity = communityRecordService.queryApRecordGainById(drawTaskRelationRecordEntity.getRecordId());
                        superviseExtractRecordGainView = new SuperviseExtractRecordGainView();
                        superviseExtractRecordGainView.setApSuperTaskId(drawTaskRelationRecordEntity.getApSuperTaskId());
                        superviseExtractRecordGainView.setRecordId(drawTaskRelationRecordEntity.getRecordId());
                        superviseExtractRecordGainView.setCqStatus(drawTaskRelationRecordEntity.getCqStatus());
                        superviseExtractRecordGainView.setRecordProjectName(apRecordGainEntity.getRecordProjectName());
                        superviseExtractRecordGainView.setRecordProjectAddress(apRecordGainEntity.getRecordProjectAddress());
                        superviseExtractRecordGainView.setStatus(apRecordGainEntity.getStatus());
                        superviseExtractRecordGainView.setReportStatus(apRecordGainEntity.getReportStatus());
                        superviseExtractRecordGainView.setRecordUser(apRecordGainEntity.getRecordUser());
                        superviseExtractRecordGainView.setRecordLevel(apRecordGainEntity.getRecordLevel());
                        superviseExtractRecordGainView.setRecordUnitName(apRecordGainEntity.getRecordUnitName());
                        superviseExtractRecordGainView.setLocation(location);
                        superviseExtractRecordGainViews.add(superviseExtractRecordGainView);
                    }else if(drawTaskRelationRecordEntity.getSuperType().equals(Constant.AP_WORK)){
                        apRecordEntity = apRecordService.queryApRecordById(drawTaskRelationRecordEntity.getRecordId());
                        superviseExtractRecordView = new SuperviseExtractRecordView();
                        superviseExtractRecordView.setApSuperTaskId(drawTaskRelationRecordEntity.getApSuperTaskId());
                        superviseExtractRecordView.setRecordId(drawTaskRelationRecordEntity.getRecordId());
                        superviseExtractRecordView.setCqStatus(drawTaskRelationRecordEntity.getCqStatus());
                        superviseExtractRecordView.setRecordProjectName(apRecordEntity.getRecordProjectName());
                        superviseExtractRecordView.setRecordProjectAddress(apRecordEntity.getRecordProjectAddress());
                        superviseExtractRecordView.setStatus(apRecordEntity.getStatus());
                        superviseExtractRecordView.setReportStatus(apRecordEntity.getReportStatus());
                        superviseExtractRecordView.setRecordUser(apRecordEntity.getRecordUser());
                        superviseExtractRecordView.setRecordLevel(apRecordEntity.getRecordLevel());
                        superviseExtractRecordView.setRecordUnitName(apRecordEntity.getRecordUnitName());
                        superviseExtractRecordView.setLocation(location);
                        superviseExtractRecordViews.add(superviseExtractRecordView);
                    }

                }
                if(!StringUtils.isEmpty(superviseExtractRecordGainViews) && superviseExtractRecordGainViews.size()>0){
                    QueryResults<SuperviseExtractRecordGainView> queryResultsRecordGain = new QueryResults(superviseExtractRecordGainViews,(long)pageable.getPageSize(),pageable.getOffset(),superviseExtractRecordGainViews.size());
                    return  queryResultsRecordGain;
                }else if(!StringUtils.isEmpty(superviseExtractRecordViews) && superviseExtractRecordViews.size()>0){
                    QueryResults<SuperviseExtractRecordView> queryResultsRecord = new QueryResults(superviseExtractRecordViews,(long)pageable.getPageSize(),pageable.getOffset(),superviseExtractRecordViews.size());
                    return queryResultsRecord;
                }else {
                    return null;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
        return null;
    }

    /**
     * 修改是否抽取专家状态
     * @param taskId
     * @param recordId
     */
    @Override
    public void updSuperTask(String taskId, String recordId) {
        if(!"".equals(taskId) && taskId!=null && recordId!=null && !"".equals(recordId)){
            QDrawTaskRelationRecordEntity q = QDrawTaskRelationRecordEntity.drawTaskRelationRecordEntity;
            jpaQueryFactory.update(q)
                    .set(q.cqStatus, Constant.EXTRACT)
                    .where(q.apSuperTaskId.eq(taskId),q.recordId.eq(recordId))
                    .execute();
        }else {
            throw new RuntimeException();
        }
    }

    /**
     * 添加公告
     * @param apPublicEntity
     * @param request
     */
    @Override
    @Transactional
    public void addPulic(ApPublicEntity apPublicEntity, HttpServletRequest request) {
        if(StringUtils.isEmpty(apPublicEntity.getRelationId())){
            throw new RuntimeException("拟制公告失败");
        }
        R r = serviceUtil.getUserInfoByRequest(request);
        if(r.getCode().equals("200")){
            LoginUser userInfo =  (LoginUser)r.get("data");
            apPublicEntity.setCreateUser(userInfo.getLoginName());
            apPublicEntity.setOrganName(userInfo.getOrgName());
        }
        // 根据此请求路径+RelationId 前端axios获取信息
        apPublicEntity.setPublicUrl("/dzjap/supersiveController/getById");
        apPublicEntity.setRevokeg(Constant.REVOKE_NO);
        // 设置默认值
        apPublicEntity.setDelFlag(Constant.NO_DELETE);
        apPublicEntity.setCreateTime(new Date());
        apPublicEntity.setInputenTime(new Date());

        // 设置类型8：监管任务，根据此类型前端判断进入哪个页面
        apPublicEntity.setFictionType("8");
        publicDao.save(apPublicEntity);
        //调取待办接口
        ApTodoEntity  apTodoEntity = new ApTodoEntity();
        apTodoEntity.setStatus(Constant.STATUSDB);
        apTodoEntity.setCreateTime(new Date());
        apTodoEntity.setDelFlag(Constant.NO_DELETE);
        apTodoEntity.setTitle(apPublicEntity.getTitle()+"公开公告审批");
        apTodoEntity.setLinkId(apPublicEntity.getPublicId());
        //审批员才能看到相应的审批
        apTodoEntity.setStation("地震部门审定人员");
        //去审批页
        apTodoEntity.setUrl("../todolist/publick.html");
        apTodoEntity.setLocation(apPublicEntity.getOrganName());
        apTodoEntity.setCreatePerson(ServiceUtil.getLoginName(request));
        //审批的业务
        apTodoEntity.setTypeFlag(Constant.TYPRFLAGSS);
        apTodoService.saveDB(apTodoEntity);
        QApSupersiveTaskInfoEntity q=QApSupersiveTaskInfoEntity.apSupersiveTaskInfoEntity;
        jpaQueryFactory.update(q).set(q.enterpriseNotice,"1").where(q.id.eq(apPublicEntity.getRelationId())).execute();
    }

    @Override
    public ApSupersiveTaskInfoEntity findById(String taskId) {
        QApSupersiveTaskInfoEntity q=QApSupersiveTaskInfoEntity.apSupersiveTaskInfoEntity;
        return jpaQueryFactory.selectFrom(q).where(q.id.eq(taskId)).fetchOne();
    }

    @Override
    public List<ApRecordEntity> findRecordById(String taskId) {
        QDrawTaskRelationRecordEntity qDrawTaskRelationRecordEntity = QDrawTaskRelationRecordEntity.drawTaskRelationRecordEntity;
        List<DrawTaskRelationRecordEntity> listIds=jpaQueryFactory.
                selectFrom(qDrawTaskRelationRecordEntity)
                .where(qDrawTaskRelationRecordEntity.apSuperTaskId.eq(taskId)).fetch();
        if(!StringUtils.isEmpty(listIds)&&listIds.size()>0){
            List<ApRecordEntity> listRe=new ArrayList<>();
            for (DrawTaskRelationRecordEntity listId:listIds) {
               ApRecordEntity apRecordEntity= apRecordService.queryApRecordById(listId.getRecordId());
                listRe.add(apRecordEntity);
            }
            return listRe;
        }
        return null;
    }

    @Override
    public List<ApRecordGainEntity> finRecorGById(String taskId) {
        QDrawTaskRelationRecordEntity qDrawTaskRelationRecordEntity = QDrawTaskRelationRecordEntity.drawTaskRelationRecordEntity;
        List<DrawTaskRelationRecordEntity> listIds=jpaQueryFactory.selectFrom(qDrawTaskRelationRecordEntity)
                .where(qDrawTaskRelationRecordEntity.apSuperTaskId.eq(taskId)).fetch();
        if(!StringUtils.isEmpty(listIds)&&listIds.size()>0){
            List<ApRecordGainEntity> listApReG=new ArrayList<>();
            for (DrawTaskRelationRecordEntity entity:
                    listIds) {
                ApRecordGainEntity apRecordGainEntity=communityRecordService.queryApRecordGainById(entity.getRecordId());
                listApReG.add(apRecordGainEntity);
            }
            return listApReG;
        }
        return null;
    }


}
